<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/10
 * Time: 10:01
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class Patent extends Model
{
    /**
     * title 表名
     * @var string
     */
    protected $table = 'd_patent';  //属性


    /**
     * title 添加专利
     * @return int
     * @throws \yii\db\Exception
     */
    public function addPatent($create_time,$uid)
    {
        $id = tools::createUuid();
        $field = ['id', 'create_time','user_id'];
        $batchData = [[$id, $create_time,$uid]];
        $add = Yii::$app->db->createCommand()->batchInsert(tools::databaseName().'.'.$this->table, $field, $batchData)->execute();
        if ($add) {
            return $id;
        } else {
            return 1;
        }
    }

    /**
     * title 保存专利
     * @param $id
     * @param $project_id
     * @param $project_name
     * @param $thesis_id
     * @param $article_name
     * @param $create_time
     * @param $realpath
     * @param $author
     * @param $uid
     * @return int
     * @throws \yii\db\Exception
     */
    public function savePatent($id, $project_id, $project_name, $patent_id, $article_name, $update_time, $realpath, $author, $uid, $status,$pilot_date)
    {

        $data = ['project_id' => $project_id, 'project_name' => $project_name, 'patent_id' => $patent_id, 'article_name' => $article_name, 'update_time' => $update_time, 'image' => $realpath, 'author' => $author, 'user_id' => $uid, 'status' => $status,'pilot_date'=>$pilot_date];
        $save = Yii::$app->db->createCommand()->update(tools::databaseName() . '.' .$this->table, $data, "id = '$id'")->execute();

        return $save;
    }


    /**
     * title 专利列表
     * @param $project_id
     * @return array
     */
    public function patentList($user_id)
    {
        $query = new \yii\db\Query();
        $list = $query->select('*')->from(tools::databaseName() . '.' .$this->table)->where(['user_id' => $user_id])->all();
        foreach ($list as $k => $v) {
            //论文
            $thesis = $query->select(['content'])->from(tools::databaseName() . '.' .'patent_type')->where(['id' => $v['patent_id']])->one();
            $list[$k]['patent'] = $thesis['content'];

        }
        return $list;


    }

    /**
     * title 论文详情
     * @param $project_id
     * @return array
     */
    public function detailPatent($id)
    {
        $query = new \yii\db\Query();
        $list = $query->select('*')->from(tools::databaseName() . '.' .$this->table)->where(['id' => $id])->all();
        $data = [];
        foreach ($list as $k => $v) {
            //论文
            $thesis = $query->select(['content'])->from(tools::databaseName() . '.' .'patent_type')->where(['id' => $v['patent_id']])->one();
            $v['patent'] = $thesis['content'];
            $data[] = $v;

        }

        return $data;


    }


    /**
     * title 删除论文
     * @param $id
     * @return int
     * @throws \yii\db\Exception
     */
    public function delPatent($id)
    {
        $del = Yii::$app->db->createCommand()->delete(tools::databaseName() . '.' .$this->table, "id = '$id'")->execute();

        return $del;
    }


    /**
     * title 条件搜索
     * @param $uid
     * @param $type
     * @param $status
     * @return array
     */
    public function searchThesis($uid,$type,$status)
    {
        $query = new \yii\db\Query();
        if(empty($type)) {
            $where = ['user_id' => $uid, 'status' => $status];
        }else{
            $where = ['user_id' => $uid, 'patent_id' => $type, 'status' => $status];
        }
        $list = $query->select('*')->from(tools::databaseName(). '.' .$this->table)->where($where)->all();

        foreach($list as $k => $v){
            //论文
            $thesis = $query->select(['content'])->from(tools::databaseName().'.patent_type')->where(['id'=>$v['patent_id']])->one();
            $list[$k]['patent'] = $thesis['content'];

        }
        return $list;


    }


}